#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//int main()
//{
//	//int a = 870;
//	char arr[3] = { 1,2,3 };
//	printf("%zu\n", strlen(&arr + 1));
//	return 0;
//}


//int main()
//{
//	double a = 3.4;
//	double b = 7.8;
//	double c = a - b;
//	printf("%lf\n", c);
//	return 0;
//}



int cmp_int(const void* p1, const void* p2)
{
	return *(int*)p1 - *(int*)p2;
}

void my_printf(int arr[], int sz)
{
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

//void test_int()
//{
//	int arr[] = { 1,9,5,8,7,2,11,33,55,4 };
//	int sz = sizeof(arr) / sizeof(arr[0]);
//	my_printf(arr, sz);
//	qsort(arr, sz, sizeof(arr[0]), cmp_int);
//	my_printf(arr, sz);
//}

typedef struct STR
{
	char name[30];
	int age;
}stu;

int cmp_stu_name(const void* p1, const void* p2)
{
	return strcmp(((stu*)p1)->name, ((stu*)p2)->name);
}

void my_printf_stu(stu* arr, int sz)
{
	for (int i = 0; i < sz; i++)
	{
		printf("%s:%d\n",(arr+i)->name,(arr+i)->age );
	}
	printf("\n");
}

int cmp_stu_age(const void* p1, const void* p2)
{
	return ((stu*)p1)->age - ((stu*)p2)->age;
}

//void test_stu()
//{
//	stu arr[] = { {"mengyou",20},{"sesmall",18},{"chengzi",15},{"banzhang",19} };
//	int sz = sizeof(arr) / sizeof(arr[0]);
//	//qsort(arr, sz, sizeof(arr[0]), cmp_stu_name);
//	qsort(arr, sz, sizeof(arr[0]), cmp_stu_age);
//	my_printf_stu(arr, sz);
//}

void Swamp(char* digt1, char* digt2, size_t width)
{
	for (int i = 0; i < width; i++)
	{
		char tmp = *digt1;
		*digt1 = *digt2;
		*digt2 = tmp;
		digt1++;
		digt2++;
	}
}

void my_Bsort(void* base, size_t sz, size_t width, int (*cmp)(const void* p1, const void* p2))
{
	for (int i = 0; i < sz; i++)
	{
		for (int j = 0; j < sz - 1 - i; j++)
		{
			//arr[j],arr[j+1]
			if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
			{
				Swamp((char*)base + j * width, (char*)base + (j + 1) * width, width);
			}
		}
	}
}

//void test_Bsort()
//{
//	int arr[] = { 1,9,5,8,7,2,11,33,55,4 };
//	int sz = sizeof(arr) / sizeof(arr[0]);
//	my_printf(arr, sz);
//	my_Bsort(arr, sz, sizeof(arr[0]), cmp_int);
//	my_printf(arr, sz);
//}

void test_Bsort2()
{
	stu arr[] = { {"mengyou",20},{"sesmall",18},{"chengzi",15},{"banzhang",19} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	my_Bsort(arr, sz, sizeof(arr[0]), cmp_stu_name);
	//my_Bsort(arr, sz, sizeof(arr[0]), cmp_stu_age);
	my_printf_stu(arr, sz);
}

int main()
{
	//test_int();
	//test_stu();
	//test_Bsort();
	test_Bsort2();
	return 0;
}


